<?php /* Smarty version Smarty-3.0.6, created on 2011-10-15 02:09:49
         compiled from ".\templates\s028_1.dwt" */ ?>
<?php /*%%SmartyHeaderCode:179924e98eb6d4b9751-63137363%%*/if(!defined('SMARTY_DIR')) exit('no direct access allowed');
$_smarty_tpl->decodeProperties(array (
  'file_dependency' => 
  array (
    '77cdabe66a3ac9741c1e8beabc4cc47b59148e18' => 
    array (
      0 => '.\\templates\\s028_1.dwt',
      1 => 1318644587,
      2 => 'file',
    ),
  ),
  'nocache_hash' => '179924e98eb6d4b9751-63137363',
  'function' => 
  array (
  ),
  'has_nocache_code' => false,
)); /*/%%SmartyHeaderCode%%*/?>
<?php if (!is_callable('smarty_modifier_capitalize')) include 'D:\study\smarty\plugins\modifier.capitalize.php';
if (!is_callable('smarty_modifier_date_format')) include 'D:\study\smarty\plugins\modifier.date_format.php';
if (!is_callable('smarty_modifier_escape')) include 'D:\study\smarty\plugins\modifier.escape.php';
if (!is_callable('smarty_modifier_replace')) include 'D:\study\smarty\plugins\modifier.replace.php';
if (!is_callable('smarty_modifier_spacify')) include 'D:\study\smarty\plugins\modifier.spacify.php';
if (!is_callable('smarty_modifier_truncate')) include 'D:\study\smarty\plugins\modifier.truncate.php';
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- TemplateBeginEditable name="doctitle" -->
<title><?php echo $_smarty_tpl->getVariable('title')->value;?>
</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
<!-- TemplateEndEditable -->
<!-- TemplateBeginEditable name="head" -->
<!-- TemplateEndEditable -->
</head>

<body>
<?php $_template = new Smarty_Internal_Template("top.tpl", $_smarty_tpl->smarty, $_smarty_tpl, $_smarty_tpl->cache_id, $_smarty_tpl->compile_id, null, null);
$_template->assign('title','title'); echo $_template->getRenderedTemplate();?><?php $_template->updateParentVariables(0);?><?php unset($_template);?>
<div class="text">
<b>变量操作符</b>
是smarty中的一些固有函数能够大大方便我们开发的灵活性和弥补php。<br />
我们今天要讲解的20个变量操作符分别是：<br />
<strong>capitalize</strong> [首字母大写]<br /> 
<strong>count_characters</strong> [计算字符数]<br /> 
<strong>cat</strong> [连接字符串]<br /> 
<strong>count_paragraphs</strong> [计算段落数]<br />
<strong>count_sentences</strong> [计算句数]<br />
<strong>count_words</strong> [计算词数]<br />
<strong>date_format</strong> [时间格式]<br />
<strong>default</strong> [默认]<br />
<strong>escape</strong> [转码]<br />
<strong>indent</strong>[缩进]<br />
<strong>lower</strong>[小写 ]<br />
<strong>nl2br</strong>[换行符替换成]<br />
<strong>regex_replace</strong>[正则替换]<br />
<strong>replace</strong>[替换]<br />
<strong>spacify</strong>[插空]<br />
<strong>string_format</strong>[字符串格式化]<br />
<strong>strip</strong>[去除(多余空格)]<br />
<strong>strip_tags</strong>[去除html标签]<br />
<strong>truncate</strong>[截取]<br />
<strong>upper</strong>[大写]<br />
<strong>wordwrap</strong>[行宽约束]<br />
我们用实例来演示他们的使用方式<br />
首先我们在php文件中需要加入下面的代码<br />
<div class="code">
&lt;?php <br />
include('smarty_c.php');<br />
$smarty-&gt;assign(&quot;title&quot;,&quot;php学习笔记028&quot;);<br />
$smarty-&gt;assign(&quot;tip&quot;,&quot;028 smarty的变量操作符&quot;);<br />
//==================引入配置文件=========================//<br />
$smarty-&gt;assign(&quot;text&quot;,&quot;this is a test. You can see the PHP's &lt;a href='#'&gt;smarty_&lt;/a&gt; !	&quot;);<br />
$smarty-&gt;display(&quot;s028_1.dwt&quot;);<br />
?&gt;</div>

我们使用不同的变量操作来处理这句话<br />
<div class="code">
原始句未处理：{$text}<hr>
capitalize[首字母大写]:{$text|capitalize}<br />
count_characters[计算字符数]:{$text|count_characters}<br />
cat[连接字符串]:{$text|cat:&quot;被连接的字符串&quot;}<br />
count_paragraphs[计算段落数]:{$text|count_paragraphs}<br />
count_sentences[计算句数]:{$text|count_sentences}<br />
count_words[计算词数]:{$text|count_words}<br />
date_format[时间格式]:{$smarty.now|date_format:&quot;%Y-%m-%d&quot;}<br />
default[默认]:{$text2|default:&quot;没有可用的值&quot;}<br />
escape[转码]：{$text|escape:htmlall}&lt;!--参数：html/url/htmlall--&gt;<br />
indent[缩进]:{$text|indent:2:'_'}<hr>
lower[小写]:{$text|lower}<br />
nl2br[换行符替换成]{$text|nl2br:&quot;&quot;}<br />
regex_replace[正则替换]:暂时跳过。<br />
replace[替换]：{$text|replace:&quot;a&quot;:&quot;[A]&quot;}<br />
spacify[插空]:{$text|spacify:&quot;*&quot;}<br />
string_format[字符串格式化]:{$text|string_format:&quot;%.2f&quot;}&lt;!--参数：%d/%.9f--&gt;<br />
strip[去除(多余空格)]:{$text|strip:&quot;_&quot;}<br />
strip_tags[去除html标签]:{$text|strip_tags}<br />
truncate[截取]:{$text|truncate:30:&quot;...&quot;}<br />
upper[大写]{$text|upper}<br />
wordwrap[行宽约束]:{$text|wordwrap:20:&quot;&quot;}</div>
我们看他们合成的效果：
<div class="effect">
原始句未处理：<?php echo $_smarty_tpl->getVariable('text')->value;?>
<hr>
<strong>capitalize[首字母大写]</strong>:<?php echo smarty_modifier_capitalize($_smarty_tpl->getVariable('text')->value);?>
<br />
<strong>count_characters[计算字符数]</strong>:<?php echo ((mb_detect_encoding($_smarty_tpl->getVariable('text')->value, 'UTF-8, ISO-8859-1') === 'UTF-8') ? preg_match_all('#[^\s\pZ]#u', $_smarty_tpl->getVariable('text')->value, $tmp) : preg_match_all('/[^\s]/',$_smarty_tpl->getVariable('text')->value, $tmp));?>
<br />
<strong>cat[连接字符串]</strong>:<?php echo ($_smarty_tpl->getVariable('text')->value).("被连接的字符串");?>
<br />
<strong>count_paragraphs[计算段落数]</strong>:<?php echo (preg_match_all('#[\r\n]+#', $_smarty_tpl->getVariable('text')->value, $tmp)+1);?>
<br />
<strong>count_sentences[计算句数]</strong>:<?php echo preg_match_all('/[^\s]\.(?!\w)/', $_smarty_tpl->getVariable('text')->value, $tmp);?>
<br />
<strong>count_words[计算词数]</strong>:<?php echo ((mb_detect_encoding($_smarty_tpl->getVariable('text')->value, 'UTF-8, ISO-8859-1') === 'UTF-8') ? preg_match_all('#[\w\pL]+#u', $_smarty_tpl->getVariable('text')->value, $tmp) : preg_match_all('#\w+#',$_smarty_tpl->getVariable('text')->value, $tmp));?>
<br />
<strong>date_format[时间格式]</strong>:<?php echo smarty_modifier_date_format(time(),"%Y-%m-%d");?>
<br />
<strong>default[默认]</strong>:<?php echo (($tmp = @$_smarty_tpl->getVariable('text2')->value)===null||$tmp==='' ? "没有可用的值" : $tmp);?>
<br />
<strong>escape[转码]</strong>：<?php echo smarty_modifier_escape($_smarty_tpl->getVariable('text')->value,'htmlall');?>
<!--html/url/htmlall--><br />
<strong>indent[缩进]</strong>:<?php echo preg_replace('!^!m',str_repeat('_',2),$_smarty_tpl->getVariable('text')->value);?>
<hr>
<strong>lower[小写]</strong>:<?php echo ((mb_detect_encoding($_smarty_tpl->getVariable('text')->value, 'UTF-8, ISO-8859-1') === 'UTF-8') ? mb_strtolower($_smarty_tpl->getVariable('text')->value,SMARTY_RESOURCE_CHAR_SET) : strtolower($_smarty_tpl->getVariable('text')->value));?>
<br />
<strong>nl2br[换行符替换成]</strong><?php echo nl2br($_smarty_tpl->getVariable('text')->value,"<br />");?>
<br />
<strong>regex_replace[正则替换]</strong>:暂时跳过。<br />
<strong>replace[替换]</strong>：<?php echo smarty_modifier_replace($_smarty_tpl->getVariable('text')->value,"a","[A]");?>
<br />
<strong>spacify[插空]</strong>:<?php echo smarty_modifier_spacify($_smarty_tpl->getVariable('text')->value,"*");?>
<br />
<strong>string_format[字符串格式化]</strong>:<?php echo sprintf("%.2f",$_smarty_tpl->getVariable('text')->value);?>
<!--%d/%.9f--><br />
<strong>strip[去除(多余空格)]</strong>:<?php echo preg_replace('!\s+!', "_",$_smarty_tpl->getVariable('text')->value);?>
<br />
<strong>strip_tags[去除html标签]</strong>:<?php echo preg_replace('!<[^>]*?>!', ' ', $_smarty_tpl->getVariable('text')->value);?>
<br />
<strong>truncate[截取]</strong>:<?php echo smarty_modifier_truncate($_smarty_tpl->getVariable('text')->value,30,"...");?>
<br />
<strong>upper[大写]</strong><?php echo ((mb_detect_encoding($_smarty_tpl->getVariable('text')->value, 'UTF-8, ISO-8859-1') === 'UTF-8') ? mb_strtoupper($_smarty_tpl->getVariable('text')->value,SMARTY_RESOURCE_CHAR_SET) : strtoupper($_smarty_tpl->getVariable('text')->value));?>
<br />
<strong>wordwrap[行宽约束]</strong>:<?php echo wordwrap($_smarty_tpl->getVariable('text')->value,20,"<br />",false);?>
<br />
</div>
</body>
</html>
